<template>
  <el-form ref="form" :model="form" label-width="100px" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" />
    </el-form-item>
    <el-form-item label="部门负责人" width="100%" prop="manager">
      <el-select v-model="form.manager" placeholder="请选择">
        <el-option v-for="item in peopleData" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button size="small" @click="cancelHandle">取 消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="confirmHandle"
      >确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple, addDeptAPI, getOneDeptAPI, updateOneDeptAPI } from '@/api/departments'
export default {
  props: {
    id: {
      type: [String, Number],
      required: true
    },
    isedit: {
      type: Boolean,
      default: false
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 编码不重复的校验
    const checkCode = (rule, value, callback) => {
      // 添加部门时
      if (!this.isedit) {
        const res = this.originList.find(item => item.code === value)
        if (res) {
          callback(new Error('编码不能重复哦'))
        } else {
          callback()
        }
      } else {
        // 编辑部门时
        const res = this.originList.filter(item => item.code !== value).find(item => item.code === value)
        if (res) {
          callback(new Error('编码不能重复哦'))
        } else {
          callback()
        }
      }
    }
    // 同部门名称不重复的校验
    const checkName = (rule, value, callback) => {
      if (!this.isedit) {
        // 添加部门时,
        const res = this.originList.filter(item => item.pid === this.id).find(item => item.name === value)
        if (res) {
          callback(new Error('同部门下名称不能重复哦'))
        } else {
          callback()
        }
      } else {
        // 编辑时
        const res = this.originList.filter(item => item.pid === this.id).filter(item => item.name !== value).find(item => item.name === value)
        if (res) {
          callback(new Error('同部门下名称不能重复哦'))
        } else {
          callback()
        }
      }
    }
    return {
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      // 表单数据
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      formLabelWidth: '120px',
      peopleData: []// 部门负责人数据
    }
  },
  async created() {
    // 获取负责人数据
    const res2 = await getEmployeeSimple()
    this.peopleData = res2.data
    if (this.isedit) {
      // 获取某一个部门的信息
      const res = await getOneDeptAPI(this.id)
      this.form = res.data
    }
  },
  methods: {
    // 点击表单确定按钮
    async  confirmHandle() {
      this.$refs.form.validate(async bool => {
        if (!bool) return
        if (!this.isedit) {
          this.form.pid = this.id
          // 调用添加部门接口
          await addDeptAPI(this.form)
        } else {
        // 编辑状态
          await updateOneDeptAPI(this.id, this.form)
        }
        // 通知父组件
        this.$emit('confirmFn')
      })
    },
    // 点击表单取消按钮
    async cancelHandle() {
      this.$emit('confirmFn')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
